package jtacck.labmedicine;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.Toast;

/**
 * Used to consolidate methods that are used by several activities rather than
 * having repeated code in methods.
 * 
 * @author Lau
 *
 */
public class SharedMethods extends Activity {

    /**
     * Performs the actual function of logging out.
     * 
     * @param a
     *            - activity that is calling the logout
     */
    public static void logout(Activity a) {
        Intent myIntent = new Intent(a.getApplicationContext(),
                MainActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        a.startActivity(myIntent);
        MainActivity.linker.disconnect();
        Toast.makeText(a.getApplicationContext(), "You have been logged out.",
                Toast.LENGTH_LONG).show();
    }

    /**
     * Generates a logout alert for an activity.
     * 
     * @param a
     *            - activity that is requesting the alert
     * 
     * @return Returns a new AlertDialog asking to logout.
     */
    public static AlertDialog logoutAlert(final Activity a) {
        return new AlertDialog.Builder(a)
                .setMessage("Proceed to log out?")
                .setCancelable(false)
                .setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                SharedMethods.logout(a);
                            }
                        })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                }).create();
    }

}
